package com.party.web.web.controller.file;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.party.common.paging.Page;
import com.party.core.model.YesNoStatus;
import com.party.core.model.file.ImgResource;
import com.party.core.model.system.TargetType;
import com.party.core.service.file.IImgResourceService;
import com.party.web.utils.RealmUtils;
import com.party.web.web.dto.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;

/**
 * 图片资源
 */
@Controller
@RequestMapping(value = "/file/img")
public class ImgResourceController {
    @Autowired
    IImgResourceService imgResourceService;

    /**
     * 打开选择图片
     * @param compress 是否要压缩
     * @param min 最小选择数
     * @param max 最大选择数
     * @param type 业务类型
     * @param info 其他提示信息
     * @return
     */
    @RequestMapping(value = "/selImg")
    public ModelAndView selImg(String compress, Integer min, Integer max, String type, String info) {
        ModelAndView mv = new ModelAndView("resource/selImg");
        mv.addObject("compress", compress);
        mv.addObject("min", min);
        mv.addObject("max", max);
        mv.addObject("type", type);
        mv.addObject("info", info);
        return mv;
    }

    /**
     * 打开相册上传图片
     * @param id 相册id
     * @param tagId 分类id
     * @return
     */
    @RequestMapping(value = "/photo/uploadImg")
    public ModelAndView photoUpload(String compress, String id, String tagId) {
        ModelAndView mv = new ModelAndView("photo/uploadImg");
        mv.addObject("id", id);
        mv.addObject("tagId", tagId);
        return mv;
    }


    /**
     * 图片列表
     *
     * @param type
     * @param pageNo
     * @param limit
     * @return
     */
    @RequestMapping(value = "/list")
    public ModelAndView listAllView(String type, Integer pageNo, Integer limit) {
        ModelAndView mv = new ModelAndView("imgRes/list");
        return mv;
    }

    /**
     * 图片列表
     *
     * @param type
     * @param pageNo
     * @param limit
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/api/listAll")
    public AjaxResult listAll(String type, Integer pageNo, Integer limit) {
        Page page = new Page();
        page.setLimit(limit);
        page.setPage(pageNo);
        ImgResource img = new ImgResource();
        img.setDelFlag(YesNoStatus.NO.getValue());
        img.setType(type);
        List<ImgResource> imgs = imgResourceService.listPage(img, page);
        return AjaxResult.success(imgs, page);
    }

    /**
     * 保存
     *
     * @param imgs
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/api/save")
    public AjaxResult save(String imgs) {
        List<ImgResource> imgResources = JSONObject.parseArray(imgs, ImgResource.class);
        LinkedList<String> ids = new LinkedList<String>();
        for (ImgResource img : imgResources) {
            String id = imgResourceService.insert(img);
            ids.add(id);
        }
        return AjaxResult.success(ids);
    }

    /**
     * 读取列表
     *
     * @param img
     * @param pageNo
     * @param limit
     * @return
     */
    @ResponseBody
    @RequestMapping("/api/list")
    public AjaxResult list(ImgResource img, Integer pageNo, Integer limit) {
        Page page = new Page();
        page.setLimit(limit);
        page.setPage(pageNo);
        img.setDelFlag(YesNoStatus.NO.getCode().toString());
        img.setCreateBy(RealmUtils.getCurrentUser().getId());

        Map<String, Object> param = Maps.newHashMap();
        // 过滤掉photo这个type的图片
        Set<String> types = TargetType.convertSet();
        Iterator<String> iterator = types.iterator();
        while (iterator.hasNext()) {
            String type = iterator.next();
            if (TargetType.PHOTO.getCode().equals(type)) {
                iterator.remove();
                break;
            }
        }
        param.put("types", types);

        List<ImgResource> imgs = imgResourceService.listPageWithParam(img, param, page);
        if (null == imgs) {
            imgs = Lists.newArrayList();
        }

//        List<ImgResource> imgs = imgResourceService.listPage(img, page);
        return AjaxResult.success(imgs, page);
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/api/del")
    public AjaxResult del(String ids) {
        String[] idArray = ids.split(",");
        for (String id : idArray) {
            ImgResource imgResource = imgResourceService.get(id);
            imgResource.setDelFlag(YesNoStatus.YES.getCode().toString());
            imgResourceService.update(imgResource);
        }
        return AjaxResult.success();
    }
}
